package com.hr.hrto.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hr.hrto.common.R;
import com.hr.hrto.domain.Category;
import com.hr.hrto.domain.Dish;
import com.hr.hrto.domain.DishFlavor;
import com.hr.hrto.dto.DishDto;
import com.hr.hrto.service.CategoryService;
import com.hr.hrto.service.DishFlavorService;
import com.hr.hrto.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created with IntelliJ IDEA.
 *菜品管理
 * @Author: 黄瑞
 * @Date: 2022/07/07/17:19
 * @Description:
 * @version:1.0
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishControllr {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        dishService.saveWithFlavor(dishDto);
        return R.success("新增成功");
    }

    /**
     * 难点，拷贝和重新赋值
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        //构造分页构造器
        Page<Dish> pageinfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //构造条件构造器
        LambdaQueryWrapper<Dish> queryWrapper =new LambdaQueryWrapper<>();
        //添加过滤条件
        queryWrapper.like(name!=null,Dish::getName,name);
        //添加排序条件
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        //执行查询
        dishService.page(pageinfo,queryWrapper);
        //对象拷贝,原因是因为pageinfo中有两个子端没有值，所以用对象拷贝的方式去返回dto
//拷贝并排除,第一个拷贝是拷贝除了数据以外的页面信息,第一个拷贝是拷贝了分页查询的那些 页数 信息
        BeanUtils.copyProperties(pageinfo,dishDtoPage,"records");
//这里比较难理解，其思想是缺少一个name，就从records中取得相应数据的id，用id去查询数据库获得实体对象，再将其name放入dto中
        List<Dish> records = pageinfo.getRecords();
        List<DishDto> list = records.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Long categoryId = item.getCategoryId();//分类id
            //根据id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category!=null){
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }

            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);

    }
    /**
     * 根据id查询菜品信息和口味
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }
    @PutMapping
    public R<String> update(@RequestBody DishDto dishDto ){
        log.info(dishDto.toString());
        dishService.updteWithFlavor(dishDto);
        return R.success("修改成功");
    }
    @PostMapping("/status/{status}")//互联网小伙伴写的
    public R<List<Dish>> status(@PathVariable Integer status, @RequestParam List<Long> ids) {
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper();
        wrapper.in(Dish::getId,ids);
        List<Dish> list = dishService.list(wrapper);
        List<Dish> list1 = list.stream().map((item) -> {
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        dishService.updateBatchById(list1);
        return R.success(list1);
    }

//    @GetMapping("/list")
//    public R<List<Dish>> list(Dish dish){
//        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
//        queryWrapper.eq(Dish::getStatus,1);//查询在起售的商品
//        //添加一个排序条件
//        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        List<Dish> list = dishService.list(queryWrapper);
//
//        return R.success(list);
//    }
@GetMapping("/list")
public R<List<DishDto>> list(Dish dish){
    LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
    queryWrapper.eq(Dish::getStatus,1);//查询在起售的商品
    //添加一个排序条件
    queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
    List<Dish> list = dishService.list(queryWrapper);
    List<DishDto> dishDtoList = list.stream().map((item)->{
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(item,dishDto);
        Long categoryId = item.getCategoryId();//分类id
        //根据id查询分类对象
        Category category = categoryService.getById(categoryId);
        if (category!=null){
            String categoryName = category.getName();
            dishDto.setCategoryName(categoryName);
        }
        Long id = item.getId();
        LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper1);
        dishDto.setFlavors(flavors);
        return dishDto;
    }).collect(Collectors.toList());

    return R.success(dishDtoList);
}
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){//这里前端界面传输的参数是ids，所以要用IDS接受，也可改前端界面的数据为id
        log.info("删除菜品 ，id为：{}",ids);
//        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper();
//        wrapper.in(Dish::getId,ids);
        dishService.removeWithStatus(ids);
        return R.success("删除成功");
    }

}
